<?php

class cmsMethodEvaluator
{

   var $dba;
   var $rel_docs;
   var $doc_id;
   var $buildMode;
   var $medium;

   function cmsMethodEvaluator (&$dba)
   {
      $this->dba        = &$dba;
      $this->rel_docs   = array();
   }

   function callMethod ($name, $attrs,$doc_id,$buildMode,$medium)
   {
      global $cfgPaths;
      $this->doc_id = $doc_id;
      $this->buildMode = $buildMode;
      $this->medium = $medium;
      $name = strtolower($name);
      if (file_exists($cfgPaths['lib']."cms/methods/".$name.".o.inc.php"))
      {
        include_once($cfgPaths['lib']."cms/methods/".$name.".o.inc.php");
        $method = new $name($this->dba,$this->doc_id,$this->buildMode,$this->medium);
        $params = array();
        if (empty($attrs['M_PARAMS'])) {
            $params = array();
            foreach ($attrs as $n => $v) {
                if ($n[0] == 'M' && $n[1] == '_') {
                    $params[strtolower(substr($n, 2))] = $v;
                }
            }
        } else {
            $params = $this->splitParams($attrs['M_PARAMS']);
        }
        $output = $method->performAction($params);
        $this->rel_docs = $method->getRelations($this->rel_docs);
        return $output;
      }
      else
        return "<p><b>Error:</b> Method \"$name\" does not exist in path \"".$cfgPaths['lib']."cms/methods/\"!</p>\n\n";
   }

   function destroy ($lang_id)
   {
      $this->dba->delMethodLinkRelations($this->doc_id,$lang_id);
      $this->dba->addPubLinkRelations(0,$lang_id,$this->doc_id,$this->rel_docs);
      unset($this->rel_docs);
   }

   function splitParams ($str)
   {
      $pairs = preg_split("/(?<!=)'&/",$str);
      foreach ($pairs As $pair)
      {
        list($name,$value) = split("='",$pair);
        $param[$name] = preg_replace("/'$/","",$value);
      }
      return $param;
   }

}

?>
